
module libuv.uvprivate.uvwin ;

version (Windows) : 
	
		package import 
			libuv.uv ,
			libuv.uvprivate.tree ,
			core.sys.windows.windows ;

extern(C):

const  MAX_PIPENAME_LEN = 256;
const S_IFLNK  = 0xA000;

enum {
	SIGHUP = 1 ,
	SIGABRT_COMPAT   = 6 ,
	SIGKILL = 9 ,
	SIGWINCH	= 28 ,
}


// ============== need remove 

struct addrinfo{
	ubyte[32] __data;
}
struct sockaddr{
	ubyte[16] __data;
};
struct sockaddr_in{
	ubyte[16] __data;
};
struct sockaddr_in6{
	ubyte[28] __data;
};

struct addrinfoW {
	ubyte[32] __data;
}
struct sockaddr_storage{
	ubyte[128] __data;
}
alias void* SOCKET;
alias void* LPFN_ACCEPTEX;
alias void* LPFN_CONNECTEX;
alias void* LPFN_WSARECV;
alias void* LPFN_WSARECVFROM;
alias void* WSAPROTOCOL_INFOW;
// ================================

alias LONG NTSTATUS ;
alias NTSTATUS* PNTSTATUS ;

alias PVOID CONDITION_VARIABLE ;
alias CONDITION_VARIABLE* PCONDITION_VARIABLE;


alias PVOID RTL_SRWLOCK ;
alias RTL_SRWLOCK SRWLOCK;
alias RTL_SRWLOCK* PSRWLOCK;
  

struct _AFD_POLL_HANDLE_INFO {
  HANDLE Handle;
  ULONG Events;
  NTSTATUS Status;
}

alias _AFD_POLL_HANDLE_INFO AFD_POLL_HANDLE_INFO ;
alias _AFD_POLL_HANDLE_INFO* PAFD_POLL_HANDLE_INFO ;

struct _AFD_POLL_INFO {
  LARGE_INTEGER Timeout;
  ULONG NumberOfHandles;
  ULONG Exclusive;
  AFD_POLL_HANDLE_INFO Handles[1];
}
alias _AFD_POLL_INFO AFD_POLL_INFO ;
alias _AFD_POLL_INFO* PAFD_POLL_INFO ;

const  UV_MSAFD_PROVIDER_COUNT =3 ;

struct uv_buf_t {
	ULONG len ;
	uv_charptr base ;
} 

alias uv_int uv_file;
alias SOCKET uv_os_sock_t;
alias HANDLE uv_thread_t;
alias HANDLE uv_sem_t;
alias CRITICAL_SECTION uv_mutex_t;


struct uv_cond_t {
	private static struct uv_cond_fallback{
	    uv_uint waiters_count;
	    CRITICAL_SECTION waiters_count_lock;
	    HANDLE signal_event;
	    HANDLE broadcast_event;
	}
	union {
  		CONDITION_VARIABLE cond_var;
		uv_cond_fallback fallback ;
	}
} 

struct uv_rwlock_t {
	private static struct uv_rwlock_fallback{
		uv_mutex_t read_mutex_;
		uv_mutex_t write_mutex_;
		uv_uint num_readers_;
	}
	union {
  		SRWLOCK srwlock_;
		uv_rwlock_fallback fallback ;
	}
} 

struct uv_barrier_t{
	uv_uint n;
	uv_uint count;
	uv_mutex_t mutex;
	uv_sem_t turnstile1;
	uv_sem_t turnstile2;
} ;

struct uv_once_s {
	uv_ubyte ran = 0;
	HANDLE event = null ;
}
alias uv_once_s uv_once_t;

alias uv_uchar uv_uid_t;
alias uv_uchar uv_gid_t;

struct uv_lib_t{
	HMODULE handle;
	uv_charptr errmsg;
} ;

alias uv_timer_tree_s = RB_HEAD!uv_timer_s ;


mixin template UV_LOOP_PRIVATE_FIELDS(){
	HANDLE 		iocp;  
	uv_uint64	 	time; 
	uv_req_t* 	pending_reqs_tail;
	uv_handle_t* 	endgame_handles; 
	uv_timer_tree_s timers;
	uv_prepare_t* prepare_handles;
	uv_check_t* check_handles;
	uv_idle_t* idle_handles;
	uv_prepare_t* next_prepare_handle;
	uv_check_t* next_check_handle;
	uv_idle_t* next_idle_handle;
	SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT];
	uv_uint active_tcp_streams;
	uv_uint active_udp_streams;
	uv_uint64 timer_counter;
}

/*
#define UV_REQ_TYPE_PRIVATE
  UV_ACCEPT,                                                                  
  UV_FS_EVENT_REQ,                                                            
  UV_POLL_REQ,                                                               
  UV_PROCESS_EXIT,                                                            
  UV_READ,                                                                    
  UV_UDP_RECV,                                                                
  UV_WAKEUP, 
  UV_SIGNAL_REQ,
*/

mixin template UV_REQ_PRIVATE_FIELDS(){
	union {
	    struct {
	      OVERLAPPED overlapped;
	      size_t queued_bytes;
	    };
	};
	uv_req_s* next_req;
}

mixin template UV_WRITE_PRIVATE_FIELDS(){
	uv_int ipc_header;
	uv_buf_t write_buffer;
	HANDLE event_handle;
	HANDLE wait_handle;
}

mixin template UV_CONNECT_PRIVATE_FIELDS(){
	/* empty */
}

mixin template UV_SHUTDOWN_PRIVATE_FIELDS(){
	/* empty */
}

mixin template UV_UDP_SEND_PRIVATE_FIELDS(){
	/* empty */
}

mixin template UV_PRIVATE_REQ_TYPES(){
  	static struct uv_pipe_accept_s {
		mixin UV_REQ_FIELDS;
		HANDLE pipeHandle;
		uv_pipe_accept_s* next_pending;
	};
	alias uv_pipe_accept_s uv_pipe_accept_t;
	static struct uv_tcp_accept_s {
		mixin UV_REQ_FIELDS;
		SOCKET accept_socket;
		uv_char accept_buffer[sockaddr_storage.sizeof * 2 + 32];
		HANDLE event_handle;
		HANDLE wait_handle;
		uv_tcp_accept_s* next_pending;
	} ;
	alias uv_tcp_accept_s uv_tcp_accept_t;
	static struct uv_read_s {
		mixin UV_REQ_FIELDS;
		HANDLE event_handle;
		HANDLE wait_handle;
	}
	alias uv_read_s uv_read_t;
}


mixin template uv_stream_connection_fields(){
	uv_int write_reqs_pending;
	uv_shutdown_t* shutdown_req;
}


mixin template uv_stream_server_fields(){
	uv_connection_cb connection_cb;
}

mixin template UV_STREAM_PRIVATE_FIELDS(){
	uv_uint reqs_pending;
	uv_int activecnt;
	uv_read_t read_req;
	union {
		struct { 
			mixin uv_stream_connection_fields;
		};
		struct {
			mixin uv_stream_server_fields;
		};
	};
}


mixin template uv_tcp_server_fields(){
	uv_tcp_accept_t* accept_reqs;
	uv_uint processed_accepts;
	uv_tcp_accept_t* pending_accepts;
	LPFN_ACCEPTEX func_acceptex;
}

mixin template uv_tcp_connection_fields(){
  uv_buf_t read_buffer;
  LPFN_CONNECTEX func_connectex;
}


mixin template UV_TCP_PRIVATE_FIELDS(){
	SOCKET socket;
	uv_int bind_error;
	union {
		struct { 
			mixin uv_tcp_server_fields; 
		};
		struct { 
			mixin uv_tcp_connection_fields; 
		};
	};
}

mixin template UV_UDP_PRIVATE_FIELDS(){
	SOCKET socket;
	uv_uint reqs_pending;
	uv_int activecnt;
	uv_req_t recv_req;
	uv_buf_t recv_buffer;
	sockaddr_storage recv_from;
	uv_int recv_from_len;
	uv_udp_recv_cb recv_cb;
	uv_alloc_cb alloc_cb;
	LPFN_WSARECV func_wsarecv;
	LPFN_WSARECVFROM func_wsarecvfrom;
	ubyte[4]	__fixme ;
}

mixin template uv_pipe_server_fields(){
	uv_int pending_instances;
	uv_pipe_accept_t* accept_reqs;
	uv_pipe_accept_t* pending_accepts;
}

struct pending_ipc_info_s {
	WSAPROTOCOL_INFOW* socket_info;
	uv_int tcp_connection;
} ;

mixin template uv_pipe_connection_fields(){
	uv_timer_t* eof_timer;
	uv_write_t ipc_header_write_req;
	uv_int ipc_pid;
	uv_uint64 remaining_ipc_rawdata_bytes;
	uv_uchar reserved[ (void*).sizeof ];
	pending_ipc_info_s	pending_ipc_info;
	uv_write_t* non_overlapped_writes_tail;
}
 
mixin template UV_PIPE_PRIVATE_FIELDS(){
	HANDLE handle;
	WCHAR* name;
	union {
	    struct { mixin uv_pipe_server_fields; };
	    struct { mixin uv_pipe_connection_fields; };
	}
}


mixin template UV_TTY_PRIVATE_FIELDS(){
 HANDLE handle;                                                              
  union {                                                                     
    struct {                                                                  
      /* Used for readable TTY handles */                                     
      HANDLE read_line_handle;                                                
      uv_buf_t read_line_buffer;                                              
      HANDLE read_raw_wait;                                                   
      /* Fields used for translating win keystrokes into vt100 characters */  
      char last_key[8];                                                       
      uv_uchar last_key_offset;                                          
      uv_uchar last_key_len;                                             
      WCHAR last_utf16_high_surrogate;                                        
      INPUT_RECORD last_input_record;                                         
    };                                                                        
    struct {                                                                  
      /* Used for writable TTY handles */                                     
      /* utf8-to-utf16 conversion state */                                    
      uv_uint utf8_codepoint;                                            
      uv_uchar utf8_bytes_left;                                          
      /* eol conversion state */                                              
      uv_uchar previous_eol;                                             
      /* ansi parser state */                                                 
      uv_uchar ansi_parser_state;                                        
      uv_uchar ansi_csi_argc;                                            
      uv_ushort ansi_csi_argv[4];                                        
      COORD saved_position;                                                   
      WORD saved_attributes;                                                  
    };                                                                        
  };
}

mixin template UV_POLL_PRIVATE_FIELDS(){
 SOCKET socket;                                                              
  /* Used in fast mode */                                                     
  SOCKET peer_socket;                                                         
  AFD_POLL_INFO afd_poll_info_1;                                              
  AFD_POLL_INFO afd_poll_info_2;                                              
  /* Used in fast and slow mode. */                                           
  uv_req_t poll_req_1;                                                        
  uv_req_t poll_req_2;                                                        
  uv_uchar submitted_events_1;                                           
  uv_uchar submitted_events_2;                                           
  uv_uchar mask_events_1;                                                
  uv_uchar mask_events_2;                                                
  uv_uchar events;
}


mixin template UV_TIMER_PRIVATE_FIELDS(){
  RB_ENTRY!(uv_timer_s) tree_entry;                                            
  uv_uint64 due;                                                               
  uv_uint64 repeat;                                                            
  uv_uint64 start_id;                                                          
  uv_timer_cb timer_cb;
}
 
mixin template UV_ASYNC_PRIVATE_FIELDS(){
 uv_req_s async_req;                                                  
  uv_async_cb async_cb;                                                       
  /* char to avoid alignment issues */                                        
  uv_charvolatile async_sent;
}

 mixin template UV_PREPARE_PRIVATE_FIELDS(){
  uv_prepare_t* prepare_prev;                                                 
  uv_prepare_t* prepare_next;                                                 
  uv_prepare_cb prepare_cb;
}

 mixin template UV_CHECK_PRIVATE_FIELDS(){
  uv_check_t* check_prev;                                                     
  uv_check_t* check_next;                                                     
  uv_check_cb check_cb;
}


 mixin template UV_IDLE_PRIVATE_FIELDS(){
 uv_idle_t* idle_prev;                                                       
  uv_idle_t* idle_next;                                                       
  uv_idle_cb idle_cb;
}
 
mixin template UV_HANDLE_PRIVATE_FIELDS(){
  uv_handle_t* endgame_next;                                                  
  uv_uint flags;
}
 
mixin template UV_GETADDRINFO_PRIVATE_FIELDS(){
  uv_getaddrinfo_cb getaddrinfo_cb;                                           
  void* alloc;                                                                
  WCHAR* node;                                                                
  WCHAR* service;                                                             
  addrinfoW* hints;                                                    
  addrinfoW* res;                                                      
  uv_int retcode;
}

struct uv_process_exit_s {                                                  
	mixin UV_REQ_FIELDS ;                                                           
}
mixin template UV_PROCESS_PRIVATE_FIELDS(){
  uv_process_exit_s exit_req;
  BYTE* child_stdio_buffer;                                                   
  uv_err_t spawn_error;                                                       
  uv_int exit_signal;                                                            
  HANDLE wait_handle;                                                         
  HANDLE process_handle;                                                      
  uv_charvolatile exit_cb_pending;
}


 mixin template UV_FS_PRIVATE_FIELDS(){
	uv_int flags;                                                                  
  	DWORD sys_errno_;                                                           
	union {                                                                     
		/* TODO: remove me in 0.9. */                                             
		WCHAR* pathw;                                                             
		uv_int fd;                                                                   
	};                                                                          
	union {                                                                     
		struct {                                                                 
			uv_int mode;                                                               
			WCHAR* new_pathw;                                                       
			uv_int file_flags;                                                         
			uv_int fd_out;                                                             
			void* buf;                                                              
			size_t length;                                                          
			uv_int64 offset;                                                         
		};                                                                        
		struct {                                                                  
			uv_double atime;                                                           
			uv_double mtime;                                                           
		};                                                                        
	};
}
 
 mixin template UV_WORK_PRIVATE_FIELDS(){
  
}

static struct uv_fs_event_req_s {                                                  
	mixin UV_REQ_FIELDS ;                                          
} 
mixin template UV_FS_EVENT_PRIVATE_FIELDS(){
  uv_fs_event_req_s req;                                                                      
  HANDLE dir_handle;                                                          
  uv_int req_pending;                                                            
  uv_fs_event_cb cb;                                                          
  WCHAR* filew;                                                               
  WCHAR* short_filew;                                                         
  WCHAR* dirw;                                                                
  uv_charptr buffer;
}

 
 mixin template UV_SIGNAL_PRIVATE_FIELDS(){
	 RB_ENTRY!(uv_signal_s) tree_entry;                                           
  	uv_req_s signal_req;                                                 
	uv_long pending_signum;
}

uv_int uv_utf16_to_utf8(const WCHAR* utf16Buffer, size_t utf16Size,  uv_charptr utf8Buffer, size_t utf8Size);
uv_int uv_utf8_to_utf16(const uv_charptr utf8Buffer, WCHAR* utf16Buffer, size_t utf16Size);


  
